#!/bin/bash
#2017年6月16日10:33:13
#by author jfedu.net
#auto install apache and vhosts
##################
H_URL="http://mirror.bit.edu.cn/apache/httpd/"
APR_URL="http://mirrors.hust.edu.cn/apache/apr/"
H_SOFT="httpd-2.4.25.tar.bz2"
APR_SOFT="apr-1.6.2.tar.bz2"
APR_UTIL_SOFT="apr-util-1.6.0.tar.bz2"
APACHE_DIR="/usr/local/apache2/"
VHOST_FILES="httpd-vhosts.conf"
DOMAINS="$1"
NUM1=`grep -c "^Include conf/extra/httpd-vhosts.conf" $APACHE_DIR/conf/httpd.conf`
NUM2=$(grep -c "$DOMAINS" $APACHE_DIR/conf/extra/httpd-vhosts.conf)

if [ $# -eq 0 ];then
	echo -e "\033[32m--------------------------\033[0m"
	echo -e "\033[32mUsage:{Please Enter $0 www.jf1.com|www.jf2.com}\033[0m"
	exit 0
fi

if [ ! -d $APACHE_DIR ];then
	wget -c  $H_URL/$H_SOFT
	wget -c  $APR_URL/$APR_SOFT 
	wget -c  $APR_URL/$APR_UTIL_SOFT
	#Install apr for apache for
	tar -jxvf $APR_SOFT 
	cd apr-1.6.2
	./configure --prefix=/usr/local/apr
	make
	make install
	#Install apr-util for apache for
	cd ..
	tar -jxvf $APR_UTIL_SOFT 
	cd apr-util-1.6.0
	./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
	make
	make install
	
	#Install apache 
	cd ..
	tar -jxvf $H_SOFT 
	cd httpd-2.4.25
	./configure --prefix=$APACHE_DIR/ --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
	make
	make install
	pkill httpd
	pkill nginx
	$APACHE_DIR/bin/apachectl start
fi

#config vhosts for apache
if [ $NUM1 -eq 0 ];then
	echo "Include conf/extra/$VHOST_FILES" >>$APACHE_DIR/conf/httpd.conf
fi
touch $APACHE_DIR/conf/extra/$VHOST_FILES


if [ $NUM2 -eq 0 ];then
cat >>$APACHE_DIR/conf/extra/$VHOST_FILES<<EOF
<VirtualHost *:80>
    ServerAdmin support@jfedu.net
    DocumentRoot "$APACHE_DIR/htdocs/$DOMAINS"
    ServerName $DOMAINS
    ErrorLog "logs/${DOMAINS}_error_log"
    CustomLog "logs/${DOMAINS}_access_log" common
</VirtualHost>
EOF

mkdir -p $APACHE_DIR/htdocs/$DOMAINS
touch $APACHE_DIR/htdocs/$DOMAINS/index.html
cat >$APACHE_DIR/htdocs/$DOMAINS/index.html<<EOF
<html><body>
<h1>$DOMAINS It works!</h1>
<h1><font color=\"red\">$DOMAINS 京峰教育-赢取白富美！</font></h1>
</body></html>
EOF
$APACHE_DIR/bin/apachectl restart
fi
